package com.hopu.oa.service.impl.notice;

import com.hopu.oa.dao.notice.NoticeManageDao;
import com.hopu.oa.entity.management.AoaStatusList;
import com.hopu.oa.entity.management.AoaTypeList;
import com.hopu.oa.entity.notice.AoaNoticeList;
import com.hopu.oa.entity.notice.AoaNoticeUserRelation;
import com.hopu.oa.entity.user.AoaUser;
import com.hopu.oa.service.notice.NoticeManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class NoticeManageServiceImp implements NoticeManageService {
    @Autowired
    private NoticeManageDao noticeManageDao;

    @Override
    public List<AoaNoticeList> queryNoticeListById(int id, String title, int index, int size) {
        return noticeManageDao.queryNoticeListById(id, title, index, size);
    }

    @Override
    public List<AoaTypeList> findTypeList() {
        return noticeManageDao.findTypeList();
    }

    @Override
    public List<AoaStatusList> findStatusList() {
        return noticeManageDao.findStatusList();
    }

    @Override
    public AoaNoticeList queryNoticeList(int id) {
        return noticeManageDao.queryNoticeList(id);
    }

    @Override
    public int addNoticeList(AoaNoticeList noticeList) {
        return noticeManageDao.addNoticeList(noticeList);
    }

    @Override
    public int addaddNoticeUser(AoaNoticeUserRelation noticeUserRelation) {
        return noticeManageDao.addaddNoticeUser(noticeUserRelation);
    }

    @Override
    public int updateNoticeList(AoaNoticeList noticeList) {
        return noticeManageDao.updateNoticeList(noticeList);
    }

    @Override
    public int deleteNoticeList(int id) {
        return noticeManageDao.deleteNoticeList(id);
    }

    @Override
    public int deleteNoticeUser(int id) {
        return noticeManageDao.deleteNoticeUser(id);
    }


    /*
      @Description : 转发通知
      @Date   :  2021/11/22
      @Param  : [queryNoticeList]
      @return : void
     */
    @Override
    public boolean forwardInform(AoaNoticeList aoaNoticeList, AoaUser aoaUser) {

        //查询当前用户角色
        int roleId = noticeManageDao.queryRole(aoaUser.getUserId());
        System.out.println("juese=" + roleId);


        //查询下级
        List<Integer> userIds = noticeManageDao.querySubordinate(roleId, aoaUser.getDeptId());
        System.out.println("xiaji=" + userIds);


        // 转发
        for (int i = 0; i <= userIds.size() - 1; i++) {
            if (noticeManageDao.forwardInform(0, aoaNoticeList.getNoticeId(), userIds.get(i)) <= 0)
                return false;
        }

        return true;

    }

    @Override
    public boolean setNoticeUserByUserIdAndNoticeId(Integer userId, Integer noticeId) {
        AoaNoticeUserRelation aoaNoticeUserRelation = noticeManageDao.queryNoticeUserByUserIdAndNoticeId(userId, noticeId);
        if (aoaNoticeUserRelation==null){
            return noticeManageDao.forwardInform(1,noticeId,userId)>0;
        }
            return noticeManageDao.setRead(aoaNoticeUserRelation.getRelatinId()) > 0;
    }
}
